//
//  BaseViewController.m
//  ShandongPoker
//
//  Created by Developer on 2025-01-08.
//  Copyright © 2025 ShandongPoker. All rights reserved.
//

#import "BaseViewController.h"
#import "UIColor+Theme.h"

@interface BaseViewController ()

@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;

@end

@implementation BaseViewController

#pragma mark - Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置基础UI
    self.view.backgroundColor = [UIColor sp_backgroundDarkColor];
    
    [self setupNavigationBar];
    [self setupUI];
    [self bindViewModel];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 隐藏导航栏底部分割线
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}

#pragma mark - Setup Methods

- (void)setupNavigationBar {
    // 设置导航栏样式
    self.navigationController.navigationBar.barTintColor = [UIColor sp_backgroundDarkColor];
    self.navigationController.navigationBar.tintColor = [UIColor sp_chipGoldColor];
    self.navigationController.navigationBar.titleTextAttributes = @{
        NSForegroundColorAttributeName: [UIColor whiteColor],
        NSFontAttributeName: [UIFont boldSystemFontOfSize:18]
    };
    
    // 设置状态栏样式
    if (@available(iOS 13.0, *)) {
        self.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
    }
}

- (void)setupUI {
    // 子类重写此方法
}

- (void)bindViewModel {
    // 子类重写此方法
}

#pragma mark - Loading Indicator

- (void)showLoadingIndicator {
    if (!self.loadingIndicator) {
        self.loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleLarge];
        self.loadingIndicator.color = [UIColor sp_chipGoldColor];
        self.loadingIndicator.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:self.loadingIndicator];
        
        [NSLayoutConstraint activateConstraints:@[
            [self.loadingIndicator.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
            [self.loadingIndicator.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor]
        ]];
    }
    
    [self.loadingIndicator startAnimating];
    self.loadingIndicator.hidden = NO;
}

- (void)hideLoadingIndicator {
    [self.loadingIndicator stopAnimating];
    self.loadingIndicator.hidden = YES;
}

#pragma mark - Message Display

- (void)showErrorMessage:(NSString *)message {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误"
                                                                       message:message
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil];
        [alert addAction:okAction];
        
        [self presentViewController:alert animated:YES completion:nil];
    });
}

- (void)showSuccessMessage:(NSString *)message {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"成功"
                                                                       message:message
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil];
        [alert addAction:okAction];
        
        [self presentViewController:alert animated:YES completion:nil];
    });
}

#pragma mark - Memory Management

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end